<?php

namespace app\admin\controller;

use app\admin\model\Admin as AdminModel;
use think\Controller;
use think\facade\Session;

class Login extends Controller
{
    //登录页面
    public function login() {
        return $this->fetch('login');
    }

    //登录验证
    public function checkLogin() {
        if ($this->request->isAjax()) {
            $name = $this->request->post('name');
            $pass = $this->request->post('pass');

            $res = AdminModel::where(['admin_name' => $name,
                                        'admin_pwd' => $pass])->find();
            if ($res) {
                //获取登录时间
                $dates = date('Y-m-d H:i:s', time());
                Session::set('admin_name',$name);
                Session::set('dates',$dates);
                $this->success('登录成功~', 'admin/index/index');
            }else {
                $this->error('管理员账号或密码错误!');
            }
        }
    }

    //退出
    public function logout() {
        Session::delete('admin_name');
        Session::delete('dates');

        return redirect('admin/login/login');
    }
}
